<template>
	<view class="container">
		<u--image style="margin-left: 5px; margin-right: 5px; margin-top: 5px;" src="/static/head2.png"
			:showLoading="true" width="100%" height="190px" radius="10px" :fade="true" duration="450"
			mode="aspectFill" />
		<uni-card>
			<uni-section title="' 家有喜事·红花郎 ' 图片" type="line">
				<uni-forms ref="form" :modelValue="formData" label-position="top" label-width="100">
					<view style="margin-left: 10px; margin-right: 10px;">
						<u--text text="1、照片场景中须包含红花郎系列产品中任意一款或多款" size="15px" align="left" color="#606266" />
						<u--text text="2、至少一张红花郎产品购买/付款截图" size="15px" align="left" color="#606266" />
						<u--text text="3、请保留红花郎产品购买凭证，兑奖时将会进行核实" size="15px" align="left" color="#606266" />
					</view>
					<view class="example-body">
						<uni-file-picker v-model="imagesList" fileMediatype="image" :multiple="true" :limit="4"
							title="最多选择4张图片" :auto-upload="false" @select="select" />
					</view>
					<uni-forms-item label="作品标题" required name="worksTitle">
						<uni-easyinput v-model="formData.worksTitle" placeholder="请输入标题" disabled="" />
					</uni-forms-item>
					<uni-forms-item label="作品文案" required name="worksCopywriting">
						<uni-easyinput type="textarea" v-model="formData.worksCopywriting" placeholder="请输入文案" />
					</uni-forms-item>
					<view>
						<u-button shape="circle" text="发布作品" style="margin-top: 30px;" color="#b61016"
							@click="submit(formData)" />
					</view>
				</uni-forms>
			</uni-section>
		</uni-card>
	</view>
</template>
<script>
	import {
		addWorks
	} from '@/api/system/works.js'
	import {
		uploadPhoto
	} from '@/api/system/photo.js'
	import {
		log
	} from 'neo-async'
	import {
		getToken
	} from "@/utils/auth";
	import {
		getUserProfile
	} from "@/api/system/user"

	export default {
		data() {
			return {
				worksTitle: null,
				worksCopywriting: null,
				imagesList: [],
				// 表单数据
				formData: {
					worksTitle: '红花郎红红火火闹元宵',
					worksCopywriting: '',
					url1: '',
					url2: '',
					url3: '',
					url4: '',
				},
			}
		},
		onLoad: function() {
			this.getUser()
		},
		methods: {
			select(e) {
				const tempFilePaths = e.tempFilePaths;
				uni.uploadFile({
					// 设置上传的请求头部
					headers: {
						Authorization: "Bearer" + getToken()
					},
					url: 'http://81.70.88.179:8080/common/upload', //仅为示例，非真实的接口地址
					filePath: tempFilePaths[0],
					name: 'file',
					formData: {
						'user': 'test'
					},
					success: (uploadFileRes) => {
						let fileMsg = JSON.parse(uploadFileRes.data)
						let fileURL = fileMsg.url
						console.log(fileURL)
						if (!this.formData.url1) {
							this.formData.url1 = fileURL
						} else if (!this.formData.url2) {
							this.formData.url2 = fileURL
						} else if (!this.formData.url3) {
							this.formData.url3 = fileURL
						} else if (!this.formData.url4) {
							this.formData.url4 = fileURL
						}
					}
				});
			},
			submit(formData) {
				if (!this.formData.url1) {
					uni.showToast({
						title: '必须上传图片',
						icon: 'error',
						duration: 1000
					})
					return
				}
				if (!this.formData.worksTitle) {
					uni.showToast({
						title: '必须填写作品标题',
						icon: 'error',
						duration: 1000
					})
					return
				}
				if (!this.formData.worksCopywriting) {
					uni.showToast({
						title: '必须填写文案',
						icon: 'error',
						duration: 1000
					})
					return
				}
				getUserProfile().then(response => {
					if(response == '401'){
						uni.navigateTo({
							url: '/pages/login',
						});
					}
						this.userDetails = response.data
						if (this.userDetails.provinces == null || this.userDetails.city == null || this.userDetails
							.county == null) {
							uni.navigateTo({
								url: '/pages/mine/info/edit',
							});
						}
					}),
					uni.showModal({
						title: '提示',
						content: '确定要发布作品吗？',
						success: function(res) {
							if (res.confirm) {
								addWorks(formData).then(respon => {
									if (respon) {
										uni.showToast({
											title: '发布成功！',
											icon: 'success',
											duration: 1000
										})
										window.location.reload();
									}
								})
							} else if (res.cancel) {
								uni.showToast({
									title: '取消发布',
									icon: 'error',
									duration: 1000
								})
							}
						}.bind(this)
					});
			},
			getUser() {
				getUserProfile().then(response => {
					this.userDetails = response.data
					if (this.userDetails.provinces == null || this.userDetails.city == null || this.userDetails
						.county == null || this.userDetails.provinces == null) {
						uni.navigateTo({
							url: '/pages/mine/info/edit',
						});
					}
				})
			},
		},
		onReady() {},
	}
</script>
<style lang="scss">
	.example-body {
		padding: 10px;
		padding-top: 0;
	}

	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		margin-left: 10px;
	}
</style>
